import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Werewolf from Castlevania II
 */
public class Werewolf  extends Mover implements Runnable
{
    private GreenfootImage[] walkLeft;
    private GreenfootImage[] walkRight;
    private GreenfootImage[] death;
    
    private retroWorld theWorld;
    
    private int walkCounter;
    private int vSpeed;
    private int acceleration ;
    private int halfImg;
    private int jumpStrength;
    private int speed;
    
    Simon simon;
    Health health;
    boolean alive;

    private volatile Thread t;
    
    public Werewolf()
    {
        alive=true;
        walkLeft = new GreenfootImage[2];
        walkRight = new GreenfootImage[2];
        death = new GreenfootImage[4];
        speed=1;
    

    
        walkCounter = 0;
        vSpeed = 0;
        acceleration = 1;
        halfImg = getImage().getHeight()/2;
        jumpStrength = 20;
        
        walkLeft[0] = new GreenfootImage("WolfLeft1.png");
        walkLeft[1] = new GreenfootImage("WolfLeft2.png");
        walkRight[0] = new GreenfootImage("WolfLeft1.png");
        walkRight[1] = new GreenfootImage("WolfLeft2.png");
        
        for (int i = 0; i < 4; i++)
            death[i] = new GreenfootImage("CVDeath" + (i + 1) + ".png");
    }
    
    public void addedToWorld(World world)
    { 
           
        theWorld = (retroWorld)world;
        simon = theWorld.getSimon();
        health = theWorld.getHealth();
       
        t = new Thread(this);
        t.start();
    }   
    
    public void run()
    {
        while(alive)
        {
            fall();
            checkCollision();
           
          /**  Something in the checkKeys() method makes the WereWolves face backwards when the
           *    world is scrolling
           
            if(checkKeys())
        { 
           speed=2;
        
        }else{speed=1;}*/
           
        
            if (getX() < 5){
                removeFromWorld();
            }
        
            if(!alive)
                break;
            moveAround();
            cycleImage();
            
             //Can't jump through blocks
             if (theWorld.isGround(getX(), getY() - halfImg))
                vSpeed = 0;
            
            try {
                t.sleep(20);
            } catch (InterruptedException e) {}            
        }
    }
    
    private void fall()
    {
        vSpeed = vSpeed + acceleration;
        
        //detect collision with ground pixel by pixel
        for (int i = 0; i <= vSpeed; i++)
        {
            if (alive && theWorld.isGround(getX(), getY() + halfImg + i))
            {
                vSpeed = 0;
                setLocation(getX(), getY() + i);
            }

        }
        
        setLocation (getX(), (getY() + vSpeed));
    }
    
    private synchronized void checkCollision()
    {
        if (intersects(simon))
        {
            if (simon.isCurrentlyWhipping())
                removeFromWorld();
            else if (simon.isCurrentlyWhipping() == false){
                health.takeDamage();
         
            }
        }
    }
    
    private void moveAround()
    {
        //Always move toward Simon
        
        int mult=1;
        if(Greenfoot.isKeyDown("d"))
            mult=2;
        
        //if (getX() > simon.getX())
            move(-3.5*speed*mult);
        //else
          //  move(1.0*speed*mult);
            
        //and randomly jump at any moment
        int i;
        i = Greenfoot.getRandomNumber(75);
        if (i == 0 && (theWorld.isGround(getX(), getY() + halfImg)))
            jump();
    }
    
    private void cycleImage()
    {
        if (getX() % 10 == 0)
        {
            if (getX() > simon.getX())
                setImage(walkLeft[walkCounter % 2]);
            else
                setImage(walkRight[walkCounter % 2]);                
            walkCounter++;
        }        
    }
    
    private void jump()
    {                
        vSpeed = -jumpStrength;
        fall();
    }
    
    public void removeFromWorld()
    {
        try {
        
            setImage(death[0]);
            t.sleep(100);
            setImage(death[1]);
            t.sleep(100);
            setImage(death[2]);
            t.sleep(100);
            setImage(death[3]);
            t.sleep(100);
            
        } catch (InterruptedException e) {}
        t=null;
        alive=false;
        theWorld.removeObject(this);
    }
    
    private boolean checkKeys()
    {
        //Move left
        if (Greenfoot.isKeyDown("a") )
        {
            return theWorld.canScrollWorld(-1);       
        }
        
        //move right
        if (Greenfoot.isKeyDown("d") )
        {
            return theWorld.canScrollWorld(1);         
        }
        
        else
            return false;
    }
}
